package cn.gson.springboot.aop;

import cn.gson.springboot.model.dao.cusdao.MyLogDao;
import cn.gson.springboot.model.pjos.Emp;
import cn.gson.springboot.model.pjos.Mylog;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;

@Aspect
@Component
public class SysLogAspect {
    private final static Logger log = org.slf4j.LoggerFactory.getLogger(SysLogAspect.class);

    @Autowired
    MyLogDao myLogDao;//工作日志dao

    //定义切点 @Pointcut
    //在注解的位置切入代码
    @Pointcut("@annotation(cn.gson.springboot.aop.MyLog)")
    public void logPoinCut(){

    }

    //切面 配置通知
    @AfterReturning("logPoinCut()")
    public void saveSysLog(JoinPoint joinPoint){
        log.info("======================日志开始记录=====================");
        //保存日志
        Mylog mylog = new Mylog();
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Emp emp = (Emp) attributes.getRequest().getSession().getAttribute("user");
        System.out.println("emp==== "+JSON.toJSONString(emp));
        //从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature =(MethodSignature) joinPoint.getSignature();
        //获取切入点所在的方法
        Method method =signature.getMethod();

        //获取操作
        MyLog myLog = method.getAnnotation(MyLog.class);
        if (myLog != null) {
//            获取当前登录员工id
            String type=myLog.type();
            String value =myLog.value();
            mylog.setEmpId(emp);//操作人
            mylog.setType(type);//相关类型
            mylog.setContent(value);//操作内容
        }

        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest requesturl = (HttpServletRequest) requestAttributes
                .resolveReference(RequestAttributes.REFERENCE_REQUEST);
        HttpSession session = requesturl.getSession();

        //请求的参数
        Object[] args = joinPoint.getArgs();
        //将参数所在的数组转换成json
        Object[] arguments  = new Object[args.length];
        for (int i = 0; i < args.length; i++) {
            if (args[i] instanceof ServletRequest || args[i] instanceof ServletResponse || args[i] instanceof MultipartFile) {
                continue;
            }
            arguments[i] = args[i];
        }
        String params = "";
        if (arguments != null) {
            try {
                params = JSON.toJSONString(arguments);
            } catch (Exception e) {
                params = arguments.toString();
            }
        }
//        mylog.setDoParams(params);//参数列表

//        新增到数据库操作
        myLogDao.save(mylog);
    }
}

